package movie.mb;

import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import movie.ejb.EjbUser;
import movie.entity.UserAccess;
import movie.utility.JsfUtil;
import movie.utility.Utility;

@ManagedBean
@SessionScoped
public class MbUser implements Serializable {

	@EJB
	private EjbUser ejbUser;
	private MbLogin mbLogin;
	private UserAccess user;
	private List<UserAccess> listUser;

	public MbUser() {
		System.out.print("Constructor: " + getClass().getName());
	}

	@PostConstruct
	public void postConstruct() {
		mbLogin =(MbLogin) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("mbLogin");
		
		System.out.print("Post construct: " + getClass().getName());
		System.out.print(mbLogin);
	}

	public void prepareUserForEdit(UserAccess logedUser) {
		user = logedUser;
		System.out.println("user-update");
		user.setTransPassword("");
		user.setTransPasswordNew1("");
		user.setTransPasswordNew2("");
	}

	public void prepareUserForDelete(UserAccess user) {
		this.user = user;
	}

	public void prepareUserForAdd() {
		user = new UserAccess();
		user.setActive(false);
	}

	public boolean addUser() {
		//MbLogin mbLogin = (MbLogin) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("mbKorisniciBean");
		if (!ejbUser.checkUsernameAvailable(user.getUserName())) {
			JsfUtil.addValidationFailedForComponent("editForm:username", "this user exists");
			JsfUtil.failValidation();
			return false;
		} else {
			user.setPassword(user.getTransPassword());
			user.setUserType(false);
			ejbUser.addUser(user);
			JsfUtil.addInfoMessage("Data saved");
			return true;
		}
	}

	public boolean updateUser() {
		if (!Utility.isStringEmptyOrNull(user.getTransPassword())) {
			String passwordMD5 = Utility.getHash(user.getTransPassword());
			if (user.getPassword().equals(passwordMD5)) {
				if (!Utility.isStringEmptyOrNull(user.getTransPasswordNew1())
						&& !Utility.isStringEmptyOrNull((user.getTransPasswordNew2()))) {
					if (!user.getTransPasswordNew1().equals(user.getTransPasswordNew2())) {
						JsfUtil.addValidationFailedForComponent("editForm:new2password", "passwords is diffrent");
						JsfUtil.failValidation();
						return false;
					} else {
						user.setPassword(Utility.getHash(user.getTransPasswordNew1()));
					}
				} else {
					if (!(Utility.isStringEmptyOrNull(user.getTransPasswordNew1())
							&& Utility.isStringEmptyOrNull(user.getTransPasswordNew2()))) {
						JsfUtil.addValidationFailedForComponent("editForm:new2password", "passwords is diffrent");
						JsfUtil.failValidation();
						return false;
					} else {
						JsfUtil.addValidationFailedForComponent("editForm:new2password", "new password is empty");
						JsfUtil.failValidation();
					}
				}
			} else {
				JsfUtil.addValidationFailedForComponent("editForm:password", "old password is incorect");
				JsfUtil.failValidation();
				System.out.println("old password is incorect");
				return false;
			}
		}
		ejbUser.editUser(user);
		JsfUtil.addInfoMessage("Data saved");
		return true;
	}

	public void deleteUser() {
		ejbUser.deleteUser(user.getIdUserAccess());
		listUser.remove(user);
		JsfUtil.addInfoMessage("User deleted");
	}

	public void changeActiveStatus(UserAccess user) {
		ejbUser.changeActiveStatus(user.getIdUserAccess());
		user.setActive(!user.getActive());
                JsfUtil.addInfoMessage("Status changed.");

	}

	public String navigationToUpdateUser(UserAccess logedUser) {
		System.out.println("before-user-update");
		prepareUserForEdit(logedUser);
		return "user-update";
	}

	public String navigationAfterUpdateUser() {
		if (!updateUser()) {
			return null;
		}
		System.out.println("save user");
		return navigationToHome();
	}

	public String navigationToHome() {
		System.out.println("home");
		return "home?faces-redirect=true";
	}

	public String navigationToUserOverview() {
		listUser = ejbUser.getUsers();
		System.out.println("user-overview");
		return "user-overview";
	}

	public UserAccess getUser() {
		return user;
	}

	public void setUser(UserAccess user) {
		this.user = user;
	}

	public List<UserAccess> getListUser() {
		return listUser;
	}
}
